package my.jwds.p429;

import my.jwds.p589.Node;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class LevelOrder {

    public static void main(String[] args) {

        Solution obj = new Solution();
        long start = System.currentTimeMillis();
        System.out.println("运行时间:" + (System.currentTimeMillis() - start));
    }

    static class Solution {



        public List<List<Integer>> levelOrder(Node root) {
            List<List<Integer>> res = new ArrayList<>();
            if (root == null)return res;
            Deque<Object[]> queue = new LinkedList<>();
            queue.addLast(new Object[]{root,0});
            while(!queue.isEmpty()){
                Object[] objs = queue.removeFirst();
                Node node = (Node) objs[0];
                int index = (int) objs[1];
                if (res.size() == index){
                    res.add(new ArrayList<>());
                }
                res.get(index).add(node.val);
                if (node.children != null){
                    for (Node child : node.children) {
                        queue.addLast(new Object[]{child,index+1});
                    }
                }
            }
            return res;

        }

    }

    static class Solution1 {



        public List<List<Integer>> levelOrder(Node root) {
            List<List<Integer>> res = new ArrayList<>();
            if (root == null)return res;
            Deque<Node> queue = new LinkedList<>();
            queue.addLast(root);
            while(!queue.isEmpty()){
                int size = queue.size();
                List<Integer> list = new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    Node node = queue.removeFirst();
                    list.add(node.val);
                    if (node.children != null){
                        for (Node child : node.children) {
                            queue.addLast(child);
                        }
                    }
                }
                res.add(list);
            }
            return res;

        }

    }
}